var frameWasRendered = false;
var columnCount = 0;

var options = {
	'iframeUrl': '/hotel/iframeprises/id/',
	'iframeWidth': 0,
	'iframeHeight': 0
}

$(document).ready(function(){
	var nameFieldWidth   = parseInt($('#nameFieldWidth').val());
	var pricesFieldWidth = parseInt($('#pricesFieldWidth').val());
	columnCount = parseInt($('#columnsCount').val());
	
	options['iframeUrl']  += $('#hotelId').val();
	if (columnCount > 0) {
		options['iframeWidth']  = columnCount * pricesFieldWidth + nameFieldWidth + 50;
		
		if (getWidthOfClientArea() < options['iframeWidth']) {
			options['iframeWidth'] = getWidthOfClientArea() - 200; 
		}
		
		options['iframeHeight'] = getHeightOfClientArea() - 250;
	} else {
		options['iframeWidth']  = 350;
		options['iframeHeight'] = 100;
	}
	
	$('<div id="qtip-blanket">').css({
		position: 'absolute',
		top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
		left: 0,
		height: $(document).height(), // Span the full document height...
		width: '100%', // ...and full width

		opacity: 0.7, // Make it slightly transparent
		backgroundColor: 'black',
		zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
	}).appendTo(document.body).hide();
	
	$('#roomsAndPrices').qtip(
		{
	      content: {
	         title: {
	            text: '<span class="popupTxt">Номера и цены</span>',
				button: 'Закрыть'
	         },
	         text: '<div class="iframeContent" style="height: ' + options['iframeHeight'] + 'px;"></div>'
	      },
	      position: {
	         target: $(document.body), // Position it via the document body...
	         corner: 'center' // ...at the center of the viewport
	      },
	      show: {
			  when: 'click', // Show it on click
         	  solo: true // And hide all other tooltips
	      },
	      hide: false,
	      style: {
	         width: { max: options['iframeWidth'], min: options['iframeWidth'] },
	         padding: '14px',
	         border: {
	            width: 9,
	            radius: 9,
	            color: '#666666'
	         },
	         name: 'light'
	      },
	      api: {
	         beforeShow: function()
	         {
	            // Fade in the modal "blanket" using the defined show speed
	            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
	         },
			 beforeHide: function()
         	 {
            	// Fade out the modal "blanket" using the defined hide speed
            	$('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         	 },
			 onShow: function()
			 {
				if (columnCount > 0) {
					if (!frameWasRendered) {
						frameWasRendered = true;
						$('.iframeContent').html('<iframe id="winPanelId" name="winPanelName" style="overflow:auto; width:100%; height:99%;" frameborder="0" src="' + options['iframeUrl'] + '"></iframe>');
					}
				} else {
					$('.iframeContent').html('К сожалению для данного отеля не задано ни одной цены');
				}
			 }
	      }
	   });
});